
 
package com.junitUtil.spring;	
import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.weaving.DefaultContextLoadTimeWeaver;
import org.springframework.core.Ordered;
import org.springframework.instrument.classloading.LoadTimeWeaver;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.test.util.ReflectionTestUtils;
import com.junitUtil.aspect.ClassPreProcessorAgentAdapterLocal;
import com.junitUtil.baseTest.AspectBaseServiceTest;





/**
 *  自定义ContextLoader spring的加载自定义功能
 * 
 **/

public class AnnotationConfigContextLoaderDecorator  extends AnnotationConfigContextLoader {

    AnnotationConfigContextLoaderDecorator() {
    }
    
    /**
     * beanFactory使用自定义的
     */
    @Override
    protected void prepareContext(GenericApplicationContext context) {
         DecoratorBeanFactory beanFactory =   DecoratorBeanFactory.getInstance();
        ReflectionTestUtils.setField(context, "beanFactory", beanFactory);
        super.prepareContext(context);
    }
    @Override
    protected void prepareContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
         super.prepareContext(context,mergedConfig);
    }
    
	protected void customizeContext(GenericApplicationContext context) {
		//解决lazy注解导致aop失效的问题
  	  QualifierAnnotationAutowireCandidateResolver qualifierAnnotationAutowireCandidateResolver = new QualifierAnnotationAutowireCandidateResolver  () ;
      DecoratorBeanFactory beanFactory =   DecoratorBeanFactory.getInstance();
      beanFactory.setAutowireCandidateResolver(qualifierAnnotationAutowireCandidateResolver);
	}
    

    /**
     * 开启spring的aop功能
     */
    @Override
    protected void  customizeBeanFactory(DefaultListableBeanFactory beanFactory ){
    	
        Object testInstance=beanFactory.getBean(DecoratorBeanFactory.JUNIT_TEST_NAME);
        if(testInstance instanceof AspectBaseServiceTest){
//需要aop.xml的用这套代码
//          RootBeanDefinition aspectJEnableBeanDefinition = new RootBeanDefinition(AspectJWeavingEnabler.class);
//          aspectJEnableBeanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
//          beanFactory.registerBeanDefinition("aspectJWeavingEnabler", aspectJEnableBeanDefinition);
//不需要aop.xml的用这套代码
            beanFactory.registerSingleton("loadTimeWeaver", loadTimeWeaver(beanFactory.getBeanClassLoader()));
             
    		RootBeanDefinition beanDefinition = new RootBeanDefinition(AnnotationAwareAspectJAutoProxyCreator.class);
    		beanDefinition.setSource(null);
    		beanDefinition.getPropertyValues().add("order", Ordered.HIGHEST_PRECEDENCE);
    		beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    		beanFactory.registerBeanDefinition("org.springframework.aop.config.internalAutoProxyCreator", beanDefinition);
    		beanDefinition.getPropertyValues().add("exposeProxy", Boolean.TRUE);
    		
    		
        }
    }
      private LoadTimeWeaver loadTimeWeaver( ClassLoader beanClassLoader) {
          LoadTimeWeaver loadTimeWeaver=new DefaultContextLoadTimeWeaver(beanClassLoader);
          loadTimeWeaver.addTransformer(new ClassPreProcessorAgentAdapterLocal( ));
          return loadTimeWeaver;
      }
      
      
      public static void main(String[] args) {

    }
}
